Skip to main content

CANopen protocol (Linux)


Table of Contents

  1. Understanding the Basics of CANopen and ROS2
  2. Environment Setup

CANopen protocol (Linux)

Comprehensive Guide to Using CANopen in ROS2 The following is a detailed step-by-step guide on using CANopen to drive eRob in ROS2, including CAN hardware recommendations.

1. Understanding the Basics of CANopen and ROS2

CANopen: A high-level communication protocol based on CAN (Controller Area Network), widely used in industrial automation, robotic control, and other fields. It defines communication standards between devices, including node management, data transfer, synchronization, and more. ROS2: The second version of the Robot Operating System, providing a modular, distributed framework for robot software development. It supports various communication protocols and middleware.

2. Environment Setup

2.1 Installing ROS2

Ensure that ROS2 is installed. Below is an example using ROS2 Humble:

# Source ROS2 environment
source /opt/ros/humble/setup.bash

# Create a workspace
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws
colcon build
source install/setup.bash

Use the ros2_canopen package, which is the primary package for CANopen communication support in ROS2.

# Clone the ros2_canopen repository
cd ~/ros2_ws/src
git clone https://github.com/ros2-canopen/ros2_canopen.git

# Install dependencies
cd ~/ros2_ws
rosdep install --from-paths src --ignore-src -r -y

# Build the workspace
colcon build
source install/setup.bash

3. Hardware Preparation

Compatibility: Ensure the hardware supports SocketCAN, as ros2_canopen relies on SocketCAN for communication. Driver Support: Check if the selected hardware has good Linux driver support, especially for SocketCAN. Performance Requirements: Choose appropriate baud rates and the number of channels based on application scenarios, such as high-frequency communication or multi-node networks. Physical Interface: Confirm the type of physical interface (e.g., USB, PCIe, SPI) and ensure compatibility with your system. Stability and Reliability: For industrial applications, select proven industrial-grade hardware to ensure long-term stable operation. Budget: Choose cost-effective hardware based on the project budget. Beginners can opt for lower-priced and easily accessible options like USBtin or CANtact.

4. Configuring the CAN Interface

4.1 Installing Necessary Drivers

Depending on the CAN interface hardware used, install the corresponding drivers. For example, using SocketCAN:

sudo apt-get install can-utils

4.2 Setting Up the CAN Interface

Assuming the use of the can0 interface, set the baud rate (e.g., 500kbps):

sudo ip link set can0 type can bitrate 500000
sudo ip link set can0 up

Check the interface status:

ifconfig can0

5. Configuring ros2_canopen

5.1 Creating a Configuration File

Create a configuration file in the ROS2 workspace to define CANopen network parameters and node configurations.

For example, create config/canopen_config.yaml:

canopen:
interface: can0
node_id: 1
bitrate: 500000
devices:
- node_id: 2
description: "Motor Controller"
# Additional device-specific configurations
- node_id: 3
description: "Sensor"
# Additional device-specific configurations

5.2 Configuring the Object Dictionary

CANopen devices define communication objects through an Object Dictionary. Ensure that each device's Object Dictionary file (usually .eds or .xdc files) is correctly configured and referenced in the ros2_canopen configuration.

6. Launching the CANopen Node

Use the launch file provided by ros2_canopen to start the CANopen master node:

ros2 launch ros2_canopen canopen_launch.py config:=<path_to_config_file>

For example:

ros2 launch ros2_canopen canopen_launch.py config:=~/ros2_ws/src/your_package/config/canopen_config.yaml

7. Communicating with CANopen Devices Using ROS2 Nodes

The ros2_canopen package provides various interfaces to interact with CANopen devices, including:

Services: Such as node management, synchronization, etc. Topics: For real-time data transmission, such as sensor data, motor status, etc. Actions: For operations requiring feedback and results, such as position control.

7.1 Example: Controlling a Motor

Assuming a motor controller is a CANopen slave device with node ID 2. You can use the interfaces provided by ros2_canopen to publish target positions:

# Publish target position to the motor controller
ros2 topic pub /motor_controller/target_position std_msgs/Float32 "data: 1.0"

7.2 Example: Reading Sensor Data

Assuming a sensor is a CANopen slave device with node ID 3. You can subscribe to the sensor data topic:

ros2 topic echo /sensor/data

8. Troubleshooting

CAN Interface Not Started: Ensure that the CAN interface is correctly configured and in the UP state.

Node ID Conflicts: Ensure that each device on the network has a unique node ID.

Incorrect Object Dictionary Configuration: Verify that each device's Object Dictionary file is correct and matches the ros2_canopen configuration.

Communication Issues: Use the candump tool to check the data flow on the CAN bus:

candump can0

9. References

ros2_canopen GitHub Repository
SocketCAN Official Documentation
CANopen Protocol Specification
Hardware Vendor Websites:
Peak Systems
Kvaser
IXXAT
BeagleBone
Raspberry Pi
Community Forums and Support:
ROS Discourse
CANopen Community
Summary

By following the steps outlined above, you can successfully integrate and use CANopen in ROS2 to communicate with various industrial devices. Selecting the appropriate CAN interface hardware is crucial for system stability and performance. Common choices include SocketCAN-compatible devices from brands like Peak Systems, Kvaser, IXXAT, as well as various embedded development boards and industrial-grade interfaces. Depending on your specific application scenario, you may need to further adjust configuration files and develop custom ROS2 nodes to meet specific requirements.

If you encounter any issues during integration, it is recommended to refer to the relevant documentation, community forums, or seek developer support.